import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:provider/provider.dart';

import '../../utils/bt_tool_utils.dart';
import '../../widgets/pagination/bt_first_page_error_indicator.dart';
import '../../widgets/pagination/bt_first_page_progress_indicator.dart';
import '../../widgets/pagination/bt_new_page_error_indicator.dart';
import '../../widgets/pagination/bt_new_page_progress_indicator.dart';
import '../../widgets/pagination/bt_no_items_found_indicator.dart';
import '../../widgets/pagination/bt_no_more_items_indicator.dart';
import 'models/bt_home_model.dart';
import 'models/bt_local_broadcaster.dart';
import 'widgets/bt_broadcaster_item_widget.dart';

@RoutePage(name: "homeRoute")
class BtHomePage extends StatelessWidget {
  const BtHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Image.asset(
          width: double.infinity,
          'assets/images/bt_home_full_bg.png',
          fit: BoxFit.fill,
        ),
        Positioned(
            left: 19,
            top: 55,
            child: Row(
              children: [
                Image.asset(
                  width: 72,
                  height: 24,
                  'assets/images/bt_today_logo.png',
                  fit: BoxFit.cover,
                ),
                const SizedBox(width: 5),
                Text(
                  getCurrentDate(),
                  style:
                      const TextStyle(color: Color(0xFF202020), fontWeight: FontWeight.normal, fontSize: 14),
                ),
              ],
            )),
        Positioned(
          left: 0,
          top: 90,
          child: Image.asset(
            height: 294,
            'assets/images/bt_brew_tunes_logo.png',
            fit: BoxFit.fitWidth,
          ),
        ),
        Container(
          width: double.infinity,
          margin: const EdgeInsets.only(left: 18, top: 358, right: 18, bottom: 8),
          decoration: BoxDecoration(
            color: const Color(0xFFFFFFFF),
            borderRadius: BorderRadius.circular(8),
          ),
          child: ChangeNotifierProvider(
            create: (context) => BtHomeModel(),
            child: Consumer<BtHomeModel>(builder: (context, model, _) {
              return RefreshIndicator(
                onRefresh: () => Future.sync(
                  () => model.pagingController.refresh(),
                ),
                child: MediaQuery.removePadding(
                  context: context,
                  removeTop: true,
                  child: PagedListView<int, BtLocalBroadcaster>(
                    pagingController: model.pagingController,
                    builderDelegate: PagedChildBuilderDelegate<BtLocalBroadcaster>(
                      itemBuilder: (context, item, index) {
                        return BtBroadcasterItemWidget(
                          index: index,
                          item: item,
                          broadcasters: model.broadcasters,
                        );
                      },
                      firstPageErrorIndicatorBuilder: (_) => BtFirstPageErrorIndicator(
                        error: model.pagingController.error,
                        onTryAgain: () => model.pagingController.refresh(),
                      ),
                      newPageErrorIndicatorBuilder: (_) => BtNewPageErrorIndicator(
                        error: model.pagingController.error,
                        onTap: () => model.pagingController.retryLastFailedRequest(),
                      ),
                      firstPageProgressIndicatorBuilder: (_) => const BtFirstPageProgressIndicator(),
                      newPageProgressIndicatorBuilder: (_) => const Padding(
                        padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                        child: BtNewPageProgressIndicator(),
                      ),
                      noItemsFoundIndicatorBuilder: (_) => const Padding(
                        padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                        child: BtNoItemsFoundIndicator(),
                      ),
                      noMoreItemsIndicatorBuilder: (_) => const Padding(
                        padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                        child: BtNoMoreItemsIndicator(),
                      ),
                    ),
                  ),
                ),
              );
            }),
          ),
        ),
      ],
    );
  }
}
